package com.sec.android.app.myfiles.update;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.dialog.UpdateDialogFragment;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.SpinnerProgressCmd;
import com.sec.android.app.myfiles.info.AppConstants.AppUpdateBadge;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;

public class APKDownloadManager {
    private Context mContext;
    private static APKDownloadManager sInstance = null;
    private static boolean sVersionChecking = false;

    public static APKDownloadManager getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new APKDownloadManager(context);
        }
        return sInstance;
    }

    private APKDownloadManager(Context context) {
        mContext = context.getApplicationContext();
    }

    public void setVersionChecking(boolean versionChecking) {
        sVersionChecking = versionChecking;
    }

    public boolean getVersionChecking() {
        return sVersionChecking;
    }

    public static class Result {
        public boolean needMyFilesUpdate;
        public boolean needCloudUpdate;
        public boolean noCheck;

        Result(boolean needMyFilesUpdate, boolean needCloudUpdate, boolean noCheck) {
            this.needMyFilesUpdate = needMyFilesUpdate;
            this.needCloudUpdate = needCloudUpdate;
            this.noCheck = noCheck;
        }

        public boolean hasUpdate() {
            return needMyFilesUpdate || needCloudUpdate;
        }
    }

    public boolean showUpdateDialog(int processId, Activity activity, final FragmentManager fragmentManager, UpdateChecker.StubUpdateCheckListener listener) {
        Result result = needVersionUpdate(listener, false);
        Log.d(this, "showUpdateDialog : " + result.noCheck + " " + result.needMyFilesUpdate + " " + result.needCloudUpdate);
        boolean needUpdate = result.hasUpdate();
        if (needUpdate) {
            if (result.noCheck) {
                if (!UiUtils.isNetworkOn(mContext)) {
                    Toast.makeText(mContext, R.string.need_update, Toast.LENGTH_LONG).show();
                } else {
                    setVersionChecking(true);
                    UpdateDialogFragment.updateDialogShow(fragmentManager, result.needCloudUpdate);
                }
            } else {
                setVersionChecking(true);
                MyFilesFacade.execSpinnerProgress(processId, activity, SpinnerProgressCmd.OperationProgress.SHOW);
            }
        }
        return needUpdate;
    }

    public Result needVersionUpdateAboutMyFiles(UpdateChecker.StubUpdateCheckListener listener, boolean checkMyFilesOnly) {
        Result result = needVersionUpdate(listener, checkMyFilesOnly);
        Log.d(this, "needVersionUpdateAboutMyFiles : noCheck - " + result.noCheck + " ,needMyFilesUpdate - " + result.needMyFilesUpdate + " ,needCloudUpdate - " + result.needCloudUpdate);
        return result;
    }

    private Result needVersionUpdate(UpdateChecker.StubUpdateCheckListener listener, boolean checkMyFilesOnly) {
        UpdateChecker updateChecker = UpdateChecker.getInstance(mContext.getApplicationContext());
        updateChecker.setLoadingProgress(false);
        Result updateCheckResult = updateChecker.needUpdate(checkMyFilesOnly);
        int updateBadgeCnt = PreferenceUtils.getBadgeCount(mContext);

        if (updateCheckResult.hasUpdate()) {
            if (updateBadgeCnt != AppUpdateBadge.EXIST_NEW_APP_UPDATE) {
                PreferenceUtils.setBadgeCount(mContext, AppUpdateBadge.EXIST_NEW_APP_UPDATE);
            }
            return updateCheckResult;
        } else {
            if (updateChecker.canServerCheck()) {
                setVersionChecking(true);
                updateChecker.getUpdateApkInfo(listener, checkMyFilesOnly);
                updateChecker.setLoadingProgress(true);
                return new Result(true, true, false);
            } else if (updateBadgeCnt != AppUpdateBadge.NO_APP_UPDATE) {
                PreferenceUtils.setBadgeCount(mContext, AppUpdateBadge.NO_APP_UPDATE);
            }
        }
        return new Result(false, false, false);
    }
}
